package com.alibaba.travel.article.service.impl;

import com.alibaba.travel.article.service.MongoService;
import com.alibaba.travel.common.model.dto.ContentEntity;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: where
 * @Date: 2023/02/28/14:46
 * @Description:
 */
@Service
public class MongoServiceImpl implements MongoService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void updateById(ContentEntity contentEntity) {
        Query query = new Query(Criteria.where("_id").is(contentEntity.getId()));
        Update update = new Update();
        update.set("_id", contentEntity.getId());
        update.set("content", contentEntity.getContent());
        mongoTemplate.upsert(query, update, ContentEntity.class, "travel");
    }

    @Override
    public Long removeById(String contentId) {
        Query query = new Query(Criteria.where("_id").is(contentId));
        DeleteResult remove = mongoTemplate.remove(query, ContentEntity.class);
        return remove.getDeletedCount();
    }
}
